package com.async.Hander;

import com.async.entity.Student;
import com.async.mapper.StudentMapper;
import com.async.util.TransactionalUntil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;

@Slf4j
@Component
public class SyncExctionHandler {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TransactionalUntil transactionalUntil;

    @Async(value = "col")
    public void syncException(Integer age, CountDownLatch childMonitor, CountDownLatch mainMonitor, List<Boolean> childResponse) {
        boolean flag = true;
        // 开启事务
        TransactionStatus transactionStatus = transactionalUntil.begin();
        try {
            Student student = new Student();
            student.setName(UUID.randomUUID().toString());
            student.setId(UUID.randomUUID().toString());
            student.setAge(18);
            studentMapper.insert(student);
            log.info("ssssssssssss");
            System.out.println(childMonitor.getCount());

            if (age == 3 || age == 4) {
                int a = 1 / 0;  //报错
            }
            childMonitor.countDown();
            mainMonitor.await();
            if (childResponse.size() == 0) {
                // 事务提交
                log.info("user-线程正常执行,线程事务提交", Thread.currentThread().getName());
                transactionalUntil.commit(transactionStatus);
            } else {
                // 事务回滚
                log.info("user-线程执行出现异常,线程事务回滚", Thread.currentThread().getName());
                transactionalUntil.rollback(transactionStatus);
            }
        } catch (Exception e) {
            // 提交失败
            log.info("user-线程执行出现异常{},", Thread.currentThread().getName());
            childResponse.add(Boolean.FALSE);
            childMonitor.countDown();
            transactionalUntil.rollback(transactionStatus);
        }
//           return new AsyncResult<Boolean>(flag);
    }


    @Async(value = "col")
    public void syncException1(Integer age, CountDownLatch threadLatchs, PlatformTransactionManager transactionManager, List<TransactionStatus> statusList) {
        boolean flag = true;
        // 开启事务
        // 使用这种方式将事务状态都放在同一个事务里面
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别，开启新事务，这样会比较安全些。
        TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态

        statusList.add(status);
        Student student = new Student();
        student.setName(UUID.randomUUID().toString());
        student.setId(UUID.randomUUID().toString());
        student.setAge(18);
        studentMapper.insert(student);
        threadLatchs.countDown();
//            transactionalUntil.commit(status);
//           return new AsyncResult<Boolean>(true);
    }

}
